package cn.itcalvin.sncoal.cth.controller;

import cn.hutool.core.util.StrUtil;
import cn.itcalvin.sncoal.cth.aspect.LogAnnotation;
import cn.itcalvin.sncoal.cth.aspect.RoleAnnotation;
import cn.itcalvin.sncoal.cth.component.MinioUtil;
import cn.itcalvin.sncoal.cth.config.CthConfig;
import cn.itcalvin.sncoal.cth.config.MinioConfig;
import cn.itcalvin.sncoal.cth.pojo.common.ReqStr;
import cn.itcalvin.sncoal.cth.pojo.req.minio.ReqSearchFile;
import cn.itcalvin.sncoal.cth.pojo.req.minio.ReqUploadFile;
import cn.itcalvin.sncoal.cth.pojo.response.minio.RespListFile;
import cn.itcalvin.sncoal.cth.utils.JwtUtil;
import cn.itcalvin.vo.Message;
import cn.itcalvin.vo.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Api(tags = "文件管理")
@RestController
@RequestMapping("/api/file")
public class FileController {

    @Resource
    MinioUtil minioUtil;

    @PostMapping("/uploadMineInfo")
    @ApiOperation(value = "上传矿井基础资料")
    @RoleAnnotation(value = "4")
    @LogAnnotation(value = "上传矿井基础资料")
    public Message uploadMineInfo(@RequestParam("dicType") String dicType, @RequestParam(value = "files") MultipartFile[] files){
        final val userInfo = JwtUtil.getUserInfo();
        String filePath="mineInfo/"+userInfo.getBelongId()+"/"+dicType;
        for (MultipartFile file : files){
            minioUtil.upload(filePath, file, true);
        }
        return MessageUtil.success();
    }
    @PostMapping("/removeMineInfo")
    @ApiOperation(value = "删除矿井基础资料")
    @RoleAnnotation(value = "4")
    @LogAnnotation(value = "删除矿井基础资料")
    public Message removeMineInfo(@RequestBody @Valid ReqUploadFile reqUploadFile){
        String filePath="mineInfo/"+JwtUtil.getUserInfo().getBelongId()+"/"+reqUploadFile.getParentPath();
        return minioUtil.remove(filePath)?MessageUtil.success():MessageUtil.error("删除失败");
    }



    @PostMapping("/uploadMinePf")
    @ApiOperation(value = "上传矿井评分资料")
    @RoleAnnotation(value = "3,4")
    @LogAnnotation(value = "上传矿井评分资料")
    public Message uploadMinePf(@RequestParam(value = "file") MultipartFile file,@RequestParam(value = "taskId") Long taskId){
        String fileUrl = minioUtil.upload("minePf/"+taskId, file,false);
        return MessageUtil.success(fileUrl);
    }


    @PostMapping("/uploadMineOther")
    @ApiOperation(value = "上传富文本输入框的文件")
    @LogAnnotation(value = "上传富文本输入框的文件")
    public Message uploadMineOther(@RequestParam(value = "file") MultipartFile file){
        String fileUrl = minioUtil.upload("other", file,false);
        return MessageUtil.success(CthConfig.MINIO_URL+fileUrl);
    }

    @PostMapping("/uploadPfZbOther")
    @ApiOperation(value = "上传指标溯源富文本输入框的文件")
    @LogAnnotation(value = "上传指标溯源富文本输入框的文件")
    public Message uploadPfZbOther(@RequestParam("systemId") String systemId,@RequestParam(value = "file") MultipartFile file){
        String fileUrl = minioUtil.upload("zbOther/"+systemId, file,false);
        return MessageUtil.success(CthConfig.MINIO_URL+fileUrl);
    }



    @PostMapping("/previewFile")
    @ApiOperation(value = "预览文件")
    @LogAnnotation(value = "预览文件")
    public Message previewFile(@RequestBody @Valid ReqStr reqStr){
        final val userInfo = JwtUtil.getUserInfo();
        String preview;
        if(userInfo.getRoleType()==4){
            preview = minioUtil.preview("mineInfo/"+JwtUtil.getUserInfo().getBelongId()+"/"+reqStr.getId());
        }else{
            preview = minioUtil.preview(reqStr.getId());
        }
        return MessageUtil.success(preview);
    }

    @PostMapping("/previewFilePf")
    @ApiOperation(value = "预览文件")
    @LogAnnotation(value = "预览文件")
    public Message previewFilePf(@RequestBody @Valid ReqStr reqStr){
        return MessageUtil.success(minioUtil.preview(reqStr.getId()));
    }


    @PostMapping("/downloadFile")
    @ApiOperation(value = "下载文件")
    public void downloadFile(@RequestBody @Valid ReqStr reqStr, HttpServletResponse res){
        minioUtil.download(reqStr.getId(),res);
    }

    @PostMapping("/listObjects")
    @ApiOperation(value = "")
    @LogAnnotation(value = "根据矿井ID获取文件-矿区、专家、股份账号使用")
    @RoleAnnotation(value = "1,2,3")
    public Message<RespListFile> listObjects(@RequestBody @Valid ReqSearchFile reqSearchFile){
        return MessageUtil.success(minioUtil.listObjects(reqSearchFile.getSearch()));
    }


    @PostMapping("/listObjectsOnlyMine")
    @ApiOperation(value = "")
    @LogAnnotation(value = "根据矿井ID获取文件-矿井账号使用")
    @RoleAnnotation(value = "4")
    public Message<RespListFile> listObjectsOnlyMine(@RequestBody @Valid ReqStr reqStr){
        String prefix = "mineInfo/"+JwtUtil.getUserInfo().getBelongId()+"/"+reqStr.getId();
        return MessageUtil.success(minioUtil.listObjects(prefix));
    }
}
